package com.amao.util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.*;

/**
 * @package : com.example.util
 * @Author : yongqi
 * @DATE : 2021/4/22 17:20
 * @描述 ：
 */
public class CodeGenerator {
    private final static String DB_URL = "jdbc:mysql://10.0.0.174:3306/security_authority";
    private final static String DB_USERNAME = "myq";
    private final static String DB_PASSWORD = "123456";
    private final static String PACKAGE_NAME = "com.example";
    private final static String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";


    private final static String NULL_VALUE = "null";

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + "：");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                if (NULL_VALUE.equals(ipt)) {
                    return null;
                }
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setDriverName(DRIVER_NAME)
                .setUsername(DB_USERNAME)
                .setPassword(DB_PASSWORD)
                .setUrl(DB_URL);

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true)
                .setEntityLombokModel(true)
                .setRestControllerStyle(true)
                .setVersionFieldName("version")
                .setTablePrefix("info")
                .setControllerMappingHyphenStyle(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(Objects.requireNonNull(scanner("表名，多个表用逗号分割")).split(","));

        // gc
        gc.setActiveRecord(false)
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setAuthor("yongqi")
                .setIdType(IdType.ASSIGN_ID)
                .setSwagger2(false)
                .setOutputDir(projectPath + "/src/main/java")
                .setFileOverride(false);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(PACKAGE_NAME)
//                .setModuleName(scanner("模块名"))
                .setEntity("entity")
                .setController("controller")
                .setService("service")
                .setServiceImpl("service.impl")
                .setMapper("mappers");

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>(1);
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };


        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义mapper.xml输出
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return projectPath + "/src/main/resources/mappers/" + tableInfo.getEntityName() + ".xml";
            }
        });

        cfg.setFileOutConfigList(focList);

        // 模板引擎配置
        TemplateConfig tc = new TemplateConfig();
        tc.setXml(null);

        // 代码生成器
        new AutoGenerator().setGlobalConfig(gc)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(pc)
                .setTemplate(tc).setCfg(cfg).execute();
    }

}
